/* //device/system/broncho-ril/atchannel.h
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#ifndef ATCHANNEL_H
#define ATCHANNEL_H 1

#include "ril_util.h"
//wupeng add begin.
#include "telephony/ril.h"
//wupeng add end.

#ifdef __cplusplus
extern "C" {
#endif

/* define AT_DEBUG to send AT traffic to /tmp/radio-at.log" */
#ifdef AT_DEBUG 
extern void  AT_DUMP(const char* prefix, const char*  buff, int  len);
#else
#define  AT_DUMP(prefix,buff,len)  do{}while(0)
#endif

#define AT_ERROR_GENERIC -1
#define AT_ERROR_COMMAND_PENDING -2
#define AT_ERROR_CHANNEL_CLOSED -3
#define AT_ERROR_TIMEOUT -4
#define AT_ERROR_INVALID_THREAD -5 /* AT commands may not be issued from
                                       reader thread (or unsolicited response
                                       callback */
#define AT_ERROR_INVALID_RESPONSE -6 /* eg an at_send_command_singleline that
                                        did not get back an intermediate
										response */
#define AT_ERR_WRITE_DATA_ERROR	-7
#define DEFAULT_AT_TIMEOUT_MSEC	5000

struct _ATChannel;
typedef struct _ATChannel ATChannel;

typedef enum {
    NO_RESULT,   /* no intermediate response expected */
    NUMERIC,     /* a single intermediate response starting with a 0-9 */
    SINGLELINE,  /* a single intermediate response starting with a prefix */
    MULTILINE    /* multiple line intermediate response
                    starting with a prefix */
} ATCommandType;

/** a singly-lined list of intermediate responses */
typedef struct ATLine  {
    struct ATLine *p_next;
    char *line;
} ATLine;


/** Free this with at_response_free() */
typedef struct {
    int success;              /* true if final response indicates
                                    success (eg "OK") */
    char *finalResponse;      /* eg OK, ERROR */
    ATLine  *p_intermediates; /* any intermediate responses */
} ATResponse;

typedef enum {
    CME_ERROR_NON_CME = -1,
    CME_SUCCESS = 0,
    CME_SIM_NOT_INSERTED = 10,
    //wupeng add begin.
	CME_SIM_PIN_REQUIRED = 11,
	CME_SIM_PUK_REQUIRED = 12,
	CME_SIM_FAILURE = 13,
	CME_SIM_PIN2_REQUIRED = 17,
	CME_SIM_PUK2_REQUIRED = 18,
	CME_SIM_PINPIN2_BLOCK_ENABLE = 706,
	CME_SIM_PINPIN2_BLOCK_DISABLE = 707
    //wupeng add end.
} AT_CME_Error;

/**
 * a user-provided unsolicited response handler function
 * this will be called from the reader thread, so do not block
 * "s" is the line, and "sms_pdu" is either NULL or the PDU response
 * for multi-line TS 27.005 SMS PDU responses (eg +CMT:)
 */
typedef void (*ATUnsolHandler)(ATChannel* thiz, const char *s, 
					const char *sms_pdu, void* user_data);
typedef void (*ATTimeoutHandler) (ATChannel* thiz, void* user_data);
typedef void (*ATCloseHandler) (ATChannel* thiz, void* user_data);

ATChannel* 	at_channel_create(RilPortType type, const char* port_name,
					ATUnsolHandler on_unsol, void* unsol_data);
int			at_channel_open(ATChannel* thiz);
void		at_channel_close(ATChannel* thiz);
void		at_channel_destroy(ATChannel* thiz);

void 		at_channel_set_on_timeout(ATChannel* thiz, 
					ATTimeoutHandler on_timeout, void* timeout_data);
void 		at_channel_set_on_closed(ATChannel* thiz, 
					ATCloseHandler on_closed, void* closed_data);

int 		at_send_command_singleline(ATChannel* thiz,
									const char *command,
									long long timeoutMsec,
                                	const char *responsePrefix,
                                 	ATResponse **pp_outResponse);

int 		at_send_command_numeric (ATChannel* thiz,
									const char *command,
									long long timeoutMsec,
                                 	ATResponse **pp_outResponse);

int 		at_send_command_multiline (ATChannel* thiz,
									const char *command,
									long long timeoutMsec,
                                	const char *responsePrefix,
                                	ATResponse **pp_outResponse);

int 		at_handshake(ATChannel* thiz);

int 		at_send_command (ATChannel* thiz,
									const char *command,
									long long timeoutMsec,
									ATResponse **pp_outResponse);

int 		at_send_command_sms (ATChannel* thiz,
									const char *command, 
									const char *pdu,
									const char *responsePrefix,
									ATResponse **pp_outResponse);

int         at_send_command_no_resp (ATChannel* thiz, const char *command);

void 		at_response_free(ATResponse *p_response);
AT_CME_Error at_get_cme_error(const ATResponse *p_response);
RIL_Errno		ril_get_error_from_at_error(AT_CME_Error error);

int at_command_exec_for_result(ATChannel* c, const char* cmd, long long timeoutMsec);
int at_command_exec_singleline_for_result(ATChannel* c, const char* cmd, 
					long long timeoutMsec, const char* prefix, ATResponse **pp_outResponse);

#ifdef __cplusplus
}
#endif

#endif /*ATCHANNEL_H*/
